import { deleteLinkById, queryLinkList } from "@/api/content/link";
import { useState, useCallback, useEffect } from "react";
import { Link } from "../../../../types/content/link";
import { message } from "antd";

export const useLinkTable = () => {
  const [messageApi] = message.useMessage();
  const [loading, setLoading] = useState(false);
  const [tableData, setTableData] = useState<Link.Row[]>([]);
  /**
   * 获取标签列表
   */
  const getLinkList = useCallback(async () => {
    setLoading(true);

    const res = await queryLinkList();
    setLoading(false);
    if (res.code === 200) {
      setTableData(res.data || []);
    }
  }, []);
  /**
   * 删除
   * @param id 
   */
  const removeLink = async (id: string) => {
    const res = await deleteLinkById(id)
    if (res.code === 200) {
      messageApi.success('删除成功!')
      getLinkList()
    }
  }

  useEffect(() => {
    getLinkList();
  }, [getLinkList]);
  return {
    loading,
    tableData,
    getLinkList,
    removeLink
  }
}